package com.slicktechnologies.client.views.servicetype;

import java.util.Comparator;
import java.util.List;

import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.ProvidesKey;
import com.simplesoftwares.client.library.appstructure.SuperTable;
import com.slicktechnologies.shared.ServiceType;

public class ServicePresenterTableProxy extends SuperTable<ServiceType> {
  TextColumn<ServiceType> NameColumn;
  TextColumn<ServiceType> priceColumn;
  TextColumn<ServiceType> getStatusColumn;
  TextColumn<ServiceType> getCountColumn;
  
  public ServicePresenterTableProxy()
  {
  super();
  }
  @Override public void createTable() {
  addColumngetCount();
  addColumnName();
  addColumngetAmount();
  
  addColumngetStatus();
  }
  @Override
  protected void initializekeyprovider() {
  keyProvider= new ProvidesKey<ServiceType>()
  {
  @Override
  public Object getKey(ServiceType item)
  {
  if(item==null)
  {
  return null;
  }
  else
  return item.getId();
  }
  };
  }
  @Override
  public void setEnable(boolean state)
  {
  }
  @Override
  public void applyStyle()
  {
  }
   public void addColumnSorting(){
  addSortinggetCount();
  addSortingName();
  addSortingAmount();
  
  addSortinggetStatus();
  }
  @Override public void addFieldUpdater() {
  }
  protected void addSortinggetCount()
  {
  List<ServiceType> list=getDataprovider().getList();
  columnSort=new ListHandler<ServiceType>(list);
  columnSort.setComparator(getCountColumn, new Comparator<ServiceType>()
  {
  @Override
  public int compare(ServiceType e1,ServiceType e2)
  {
  if(e1!=null && e2!=null)
  {
  if(e1.getCount()== e2.getCount()){
  return 0;}
  if(e1.getCount()> e2.getCount()){
  return 1;}
  else{
  return -1;}
  }
  else{
  return 0;}
  }
  });
  table.addColumnSortHandler(columnSort);
  }
  protected void addColumngetCount()
  {
  getCountColumn=new TextColumn<ServiceType>()
  {
  @Override
  public String getValue(ServiceType object)
  {
  if( object.getCount()==-1)
  return "N.A";
  else return object.getCount()+"";
  }
  };
  table.addColumn(getCountColumn,"Id");
  getCountColumn.setSortable(true);
  }
  protected void addSortingName()
  {
  List<ServiceType> list=getDataprovider().getList();
  columnSort=new ListHandler<ServiceType>(list);
  columnSort.setComparator(NameColumn, new Comparator<ServiceType>()
  {
  @Override
  public int compare(ServiceType e1,ServiceType e2)
  {
  if(e1!=null && e2!=null)
  {
  if( e1.getTypeName()!=null && e2.getTypeName()!=null){
  return e1.getTypeName().compareTo(e2.getTypeName());}
  }
  else{
  return 0;}
  return 0;
  }
  });
  table.addColumnSortHandler(columnSort);
  }
  protected void addColumnName()
  {
  NameColumn=new TextColumn<ServiceType>()
  {
  @Override
  public String getValue(ServiceType object)
  {
  return object.getTypeName();
  }
  };
  table.addColumn(NameColumn,"Name");
  NameColumn.setSortable(true);
  }
  
  protected void addSortingAmount()
  {
  List<ServiceType> list=getDataprovider().getList();
  columnSort=new ListHandler<ServiceType>(list);
  columnSort.setComparator(priceColumn, new Comparator<ServiceType>()
  {
  @Override
  public int compare(ServiceType e1,ServiceType e2)
  {
  if(e1!=null && e2!=null)
  {
  if( e1.getDiagnosisAmount()!=null && e2.getDiagnosisAmount()!=null){
  return e1.getDiagnosisAmount().compareTo(e2.getDiagnosisAmount());}
  }
  else{
  return 0;}
  return 0;
  }
  });
  table.addColumnSortHandler(columnSort);
  }
  
  protected void addSortinggetStatus()
  {
  List<ServiceType> list=getDataprovider().getList();
  columnSort=new ListHandler<ServiceType>(list);
  columnSort.setComparator(getStatusColumn, new Comparator<ServiceType>()
  {
  @Override
  public int compare(ServiceType e1,ServiceType e2)
  {
  if(e1!=null && e2!=null)
  {
  if(e1.getStatus()== e2.getStatus()){
  return 0;}
  else{
  return -1;}
  }
  else{
  return 0;}
  }
  });
  table.addColumnSortHandler(columnSort);
  }
  protected void addColumngetStatus()
  {
  getStatusColumn=new TextColumn<ServiceType>()
  {
  @Override
  public String getValue(ServiceType object)
  {
  if( object.getStatus()==true)
  return "Active";
  else 
  return "In Active";
  }
  };
  table.addColumn(getStatusColumn,"Service Status");
  getStatusColumn.setSortable(true);
  }
  
  protected void addColumngetAmount()
  {
  priceColumn=new TextColumn<ServiceType>()
  {
  @Override
  public String getValue(ServiceType object)
  {
     return object.getDiagnosisAmount()+"";
  }
  };
  table.addColumn(priceColumn,"Price");
  priceColumn.setSortable(true);
  }
 
 

  
}
